package com.fcscs.cservice.client.main.configuration.inroomdining;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.CserviceControllerUtils;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.client.common.model.restaurants.BookingStatusModelData;
import com.fcscs.cservice.common.CServiceCommonEnums.RestaurantConfigType;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.user.client.ui.Widget;

public class InRoomDiningAdminConfPanel extends FcsPanel {
	private VerticalPanel vp;
	private LayoutContainer container1;
	private FieldSet resFieldSet;
	private HorizontalPanel printerHp;
	private LayoutContainer container2;
	private Label serverNameLab;
	private HorizontalPanel econnectHp;
	private LayoutContainer container3;
	private ListStore<BookingStatusModelData> requestActionStore;
	private Label requestActionLab;
	private HorizontalPanel sendRequestHp;
	private Label sendRequestLab;
	private Label serviceCodeLab;
	private HorizontalPanel emailHp;
	private LayoutContainer container4;
	private Radio emailRadio;
	private Label addressLab;
	private HorizontalPanel cConnectHp;
	private LayoutContainer container5;
	private Radio cConnectRadio;
	private Label cRequestActionLab;
	private ListStore<BookingStatusModelData> cRequestActionStore;
	private Label cServiceCodeLab;
	private FormButton saveBtn;
	private InRoomDiningAreaPanel commonConfigArea;

	private EasyClearCombobox<PropertyIdNameModelData> prosComBox;
	private ListStore<PropertyIdNameModelData> propStore;
	private HorizontalPanel propHp;
	private LayoutContainer propLc;
	private HorizontalPanel ackHp;
	private LayoutContainer container6;
	private Label timeoutLab;
	private Label emailLab;
	private TextField<String> serverNamefield;
	private EasyClearCombobox<BookingStatusModelData> requestActionCombox;
	private TextField<String> serviceCodefield;
	private TextField<String> addressfield;
	private EasyClearCombobox<BookingStatusModelData> cRequestActionCombox;
	private TextField<String> cServiceCodefield;
	private Radio printerRadio;
	private Radio econnectRadio;
	private TextField<String> timeoutField;
	private TextField<String> emailField;

	public InRoomDiningAdminConfPanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		loadInit();
	}

	private void loadInit() {
		if (null == vp) {
			vp = new VerticalPanel();
			vp.setSpacing(5);
			
			propHp = new HorizontalPanel();
			propHp.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "15px");
			propHp.setId("RestaurantConfigPanel_propHp");
			vp.add(propHp);
			
			propLc = new LayoutContainer();
			propLc.setLayout(new ColumnLayout());
			propHp.add(propLc);
			
			Label propLab = new Label(CoreConstants.CORE_CONSTANTS.property() + CServiceConstants.FIELD_SEP);
			propLab.setId("RestaurantConfigPanel_propLab");
			propLc.add(propLab, new ColumnData(100));
			
			prosComBox = new EasyClearCombobox<PropertyIdNameModelData>();
			prosComBox.focus();
			prosComBox.setDisplayField(CoreConstants.FULLTNAME);
			propStore = new ListStore<PropertyIdNameModelData>();
			prosComBox.setWidth(150);
			prosComBox.setStore(propStore);
			prosComBox.setId("RestaurantsPanel_prosComBox");
			prosComBox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
			prosComBox.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.choosePro());
			prosComBox.setTriggerAction(TriggerAction.ALL);
			prosComBox.setForceSelection(true);
			prosComBox.setFireChangeEventOnSetValue(true);
			prosComBox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {
				
				@Override
				public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
					if (null != prosComBox.getValue()) {
					}
				}
			});
			propLc.add(prosComBox);
			
			restConfigFieldSet();
			
			// TextArea
			commonConfigArea = new InRoomDiningAreaPanel();
			vp.add(commonConfigArea);
			
			saveBtn = new FormButton(CoreConstants.CORE_CONSTANTS.save());
			saveBtn.setId("RestaurantConfigPanel_saveBtn");
			saveBtn.setWidth(60);
			saveBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
				}
			});
			HorizontalPanel barPanel = new HorizontalPanel();
			HorizontalPanel leftPanel = new HorizontalPanel();
			leftPanel.setWidth(600);

			HorizontalPanel rightPanel = new HorizontalPanel();
			rightPanel.add(saveBtn);
			barPanel.add(leftPanel);
			barPanel.add(rightPanel);
			vp.add(barPanel);

			add(vp);
		}
		onShow();
	}

	private void restConfigFieldSet() {
		printerRadio = new Radio();
		econnectRadio = new Radio();
		emailRadio = new Radio();
		cConnectRadio = new Radio();

		serverNamefield = new TextField<String>();
		requestActionCombox = new EasyClearCombobox<BookingStatusModelData>();
		serviceCodefield = new TextField<String>();
		addressfield = new TextField<String>();
		cRequestActionCombox = new EasyClearCombobox<BookingStatusModelData>();
		cServiceCodefield = new TextField<String>();

		resFieldSet = new FieldSet();
		resFieldSet.setBorders(true);
		resFieldSet.setId("RestaurantConfigPanel_fieldSet");
		vp.add(resFieldSet);

		final RadioGroup group = new RadioGroup();
		group.setId("RestaurantConfigPanel_group");
		group.addListener(Events.Change, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
						// change Radio
						if ((RestaurantConfigType.PRINTER.toString())
								.equals(group.getValue().getFieldLabel())) {
							serverNamefield.setEnabled(true);
							requestActionCombox.setEnabled(false);
							serviceCodefield.setEnabled(false);
							addressfield.setEnabled(false);
							cRequestActionCombox.setEnabled(false);
							cServiceCodefield.setEnabled(false);
							// requestActionCombox.clearSelections();
							// serviceCodefield.clear();
							// addressfield.clear();
							// cRequestActionCombox.clearSelections();
							// cServiceCodefield.clear();
						}
						if ((RestaurantConfigType.E_CONNECT.toString()).equals(group.getValue()
								.getFieldLabel())) {
							serverNamefield.setEnabled(false);
							requestActionCombox.setEnabled(true);
							serviceCodefield.setEnabled(true);
							addressfield.setEnabled(false);
							cRequestActionCombox.setEnabled(false);
							cServiceCodefield.setEnabled(false);
							// serverNamefield.clear();
							// addressfield.clear();
							// cRequestActionCombox.clearSelections();
							// cServiceCodefield.clear();
						}
						if ((RestaurantConfigType.EMAIL.toString()).equals(group.getValue().getFieldLabel())) {
							serverNamefield.setEnabled(false);
							requestActionCombox.setEnabled(false);
							serviceCodefield.setEnabled(false);
							addressfield.setEnabled(true);
							cRequestActionCombox.setEnabled(false);
							cServiceCodefield.setEnabled(false);
							// serverNamefield.clear();
							// requestActionCombox.clearSelections();
							// serviceCodefield.clear();
							// cRequestActionCombox.clearSelections();
							// cServiceCodefield.clear();
						}
						if ((RestaurantConfigType.C_CONNECT.toString()).equals(group.getValue()
								.getFieldLabel())) {
							serverNamefield.setEnabled(false);
							requestActionCombox.setEnabled(false);
							serviceCodefield.setEnabled(false);
							addressfield.setEnabled(false);
							cRequestActionCombox.setEnabled(true);
							cServiceCodefield.setEnabled(true);
							// serviceCodefield.clear();
							// requestActionCombox.clearSelections();
							// serviceCodefield.clear();
							// addressfield.clear();
						}
					}
			}
		);
		

		// Send request to
		sendRequestHp = new HorizontalPanel();
		sendRequestHp.setSpacing(5);
		sendRequestHp.setId("RestaurantConfigPanel_sendRequestHp");
		resFieldSet.add(sendRequestHp);
		container1 = new LayoutContainer();
		container1.setLayout(new ColumnLayout());
		
		sendRequestHp.add(container1);
		sendRequestLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.sendRequestTo()
				+ CServiceConstants.FIELD_SEP);
		sendRequestLab.setId("RestaurantConfigPanel_serverNameLab");
		container1.add(sendRequestLab, new ColumnData(200));

		final TextField<String> sendRequestField = new TextField<String>();
		sendRequestField.setId("RestaurantConfigPanel_sendRequestField");
		container1.add(sendRequestField, new ColumnData(200));
		sendRequestField.setAllowBlank(false);
		sendRequestHp.add(container1);
		sendRequestField.addListener(Events.OnBlur, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				
			}
		});
		
		
		// printer
		printerHp = new HorizontalPanel();
		printerHp.setSpacing(5);
		printerHp.setId("RestaurantConfigPanel_printerHp");
		resFieldSet.add(printerHp);
		
		container2 = new LayoutContainer();
		container2.setLayout(new ColumnLayout());
		printerHp.add(container2);
		
		printerRadio.setId("RestaurantConfigPanel_printerRadio");
		printerRadio.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.printer()
				+ CServiceConstants.FIELD_SEP);

		printerRadio.setFieldLabel(RestaurantConfigType.PRINTER.toString());
		container2.add(printerRadio, new ColumnData(100));
		group.add(printerRadio);
		
		serverNameLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.serverName()
				+ CServiceConstants.FIELD_SEP);
		serverNameLab.setId("RestaurantConfigPanel_serverNameLab");
		container2.add(serverNameLab, new ColumnData(100));
		serverNamefield.setEnabled(false);
		serverNamefield.setId("RestaurantConfigPanel_serverNamefield");
		container2.add(serverNamefield, new ColumnData(200));
		serverNamefield.addListener(Events.OnBlur, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {

			}
		});
		
		// e-connect
		econnectHp = new HorizontalPanel();
		econnectHp.setSpacing(5);
		econnectHp.setId("RestaurantConfigPanel_econnectHp");
		resFieldSet.add(econnectHp);
		container3 = new LayoutContainer();
		container3.setLayout(new ColumnLayout());
		econnectHp.add(container3);
		
		econnectRadio.setId("RestaurantConfigPanel_econnectRadio");
		econnectRadio.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.eConnect()
				+ CServiceConstants.FIELD_SEP);
		econnectRadio.setFieldLabel(RestaurantConfigType.E_CONNECT.toString());
		container3.add(econnectRadio, new ColumnData(100));
		group.add(econnectRadio);
		
		requestActionLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.requestAction()
				+ CServiceConstants.FIELD_SEP);
		requestActionLab.setId("RestaurantConfigPanel_requestActionLab");
		container3.add(requestActionLab, new ColumnData(100));
		
		HorizontalPanel hp1 = new HorizontalPanel();
		hp1.setId("RestaurantConfigPanel_hp1");
		requestActionCombox.setId("RestaurantConfigPanel_requestActionCombox");
		requestActionStore = new ListStore<BookingStatusModelData>();
		requestActionCombox.setWidth(150);
		requestActionCombox.setEnabled(false);
		requestActionCombox.setTriggerAction(TriggerAction.ALL);
		requestActionCombox.setForceSelection(true);
		requestActionCombox.setStore(requestActionStore);
		hp1.add(requestActionCombox);
		
		container3.add(hp1, new ColumnData(200));
		serviceCodeLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.serviceCode()
				+ CServiceConstants.FIELD_SEP);
		serviceCodeLab.setId("RestaurantConfigPanel_serviceCodeLab");
		container3.add(serviceCodeLab, new ColumnData(100));
		serviceCodefield.setId("RestaurantConfigPanel_serviceCodefield");
		serviceCodefield.setEnabled(false);
		container3.add(serviceCodefield, new ColumnData(150));
		serviceCodefield.addListener(Events.OnBlur, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
			}
		});
		
		
		// email
		emailHp = new HorizontalPanel();
		emailHp.setSpacing(5);
		emailHp.setId("RestaurantConfigPanel_emailHp");
		resFieldSet.add(emailHp);
		
		container4 = new LayoutContainer();
		container4.setLayout(new ColumnLayout());
		emailHp.add(container4);
		
		emailRadio.setId("RestaurantConfigPanel_emailRadio");
		emailRadio.setBoxLabel(CoreConstants.CORE_CONSTANTS.email() + CServiceConstants.FIELD_SEP);
		emailRadio.setFieldLabel(RestaurantConfigType.EMAIL.toString());
		container4.add(emailRadio, new ColumnData(100));
		group.add(emailRadio);
		
		addressLab = new Label(ShareGWTConstants.SHARE_GWT_CONSTANTS.address() + CServiceConstants.FIELD_SEP);
		addressLab.setId("RestaurantConfigPanel_addressLab");
		container4.add(addressLab, new ColumnData(100));
		
		addressfield.setId("RestaurantConfigPanel_addressfield");
		addressfield.setEnabled(false);
		container4.add(addressfield, new ColumnData(400));
		addressfield.addListener(Events.OnBlur, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
			}
		});
		
		final String emailReg = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		addressfield.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (!value.toUpperCase().matches(emailReg)) {
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.emailErro();
				} else if (value.length() > 50) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.email(), 50);
				}
				return null;
			}
		});
		
		
		// c-connect
		cConnectHp = new HorizontalPanel();
		cConnectHp.setSpacing(5);
		cConnectHp.setId("RestaurantConfigPanel_cConnectHp");
		resFieldSet.add(cConnectHp);
		
		container5 = new LayoutContainer();
		container5.setLayout(new ColumnLayout());
		cConnectHp.add(container5);
		
		cConnectRadio.setId("RestaurantConfigPanel_cConnectRadio");
		cConnectRadio.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.cConnect()
				+ CServiceConstants.FIELD_SEP);
		cConnectRadio.setFieldLabel(RestaurantConfigType.C_CONNECT.toString());
		container5.add(cConnectRadio, new ColumnData(100));
		group.add(cConnectRadio);
		
		cRequestActionLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.requestAction()
				+ CServiceConstants.FIELD_SEP);
		cRequestActionLab.setId("RestaurantConfigPanel_cRequestActionLab");
		container5.add(cRequestActionLab, new ColumnData(100));
		
		HorizontalPanel hp2 = new HorizontalPanel();
		hp2.setId("RestaurantConfigPanel_hp2");
		cRequestActionCombox.setId("RestaurantConfigPanel_cRequestActionCombox");
		cRequestActionCombox.setEnabled(false);
		cRequestActionCombox.setTriggerAction(TriggerAction.ALL);
		cRequestActionCombox.setForceSelection(true);
		cRequestActionStore = new ListStore<BookingStatusModelData>();
		cRequestActionCombox.setWidth(150);
		cRequestActionCombox.setStore(cRequestActionStore);
		hp2.add(cRequestActionCombox);
		container5.add(hp2, new ColumnData(200));
		
		cServiceCodeLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.serviceCode()
				+ CServiceConstants.FIELD_SEP);
		cServiceCodeLab.setId("RestaurantConfigPanel_cServiceCodeLab");
		container5.add(cServiceCodeLab, new ColumnData(100));
		cServiceCodefield.setId("RestaurantConfigPanel_cServiceCodefield");
		cServiceCodefield.setEnabled(false);
		container5.add(cServiceCodefield, new ColumnData(150));
		cServiceCodefield.addListener(Events.OnBlur, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
//				if (!(RestaurantConfigType.C_CONNECT.toString()).equals(restConList.get(i)
//						.getRestConfigDetail().get(0).getConfigType())) {
//					return;
//				}
//				if (null != cServiceCodefield.getValue()) {
//					restConList.get(i).getRestConfigDetail().get(0).setNote2(cServiceCodefield.getValue());
//					restConList.get(i).getRestConfigDetail().get(0).setNote3(null);
//				}
			}
		});

		// Ack Alert
		ackHp = new HorizontalPanel();
		ackHp.setSpacing(5);
		ackHp.setId("RestaurantConfigPanel_ackHp");
		resFieldSet.add(ackHp);
		container6 = new LayoutContainer();
		container6.setLayout(new ColumnLayout());
		ackHp.add(container6);
		
		final CheckBox ackCheckBox = new CheckBox();
		ackCheckBox.setId("RestaurantConfigPanel_ackCheckBox");
		ackCheckBox.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.ecAckAlert()+CServiceConstants.FIELD_SEP);
		container6.add(ackCheckBox,new ColumnData(200));
		ackCheckBox.addListener(Events.OnClick, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				if (ackCheckBox.getValue()) {
					timeoutField.setEnabled(true);
					emailField.setEnabled(true);
				}else {
					timeoutField.setEnabled(false);
					emailField.setEnabled(false);
				}
			}
		});
		timeoutLab = new Label(CServiceConstants.CSERVICE_CONSTANTS.timeOut()
				+ CServiceConstants.FIELD_SEP);
		timeoutLab.setId("RestaurantConfigPanel_timeoutLab");
		container6.add(timeoutLab, new ColumnData(80));
		
		timeoutField = new TextField<String>();
		timeoutField.setEnabled(false);
		timeoutField.setId("RestaurantConfigPanel_timeoutField");
		container6.add(timeoutField,new ColumnData(70));
		
		emailLab = new Label(CoreConstants.CORE_CONSTANTS.email()
				+ CServiceConstants.FIELD_SEP);
		emailLab.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "50px");
		emailLab.setId("RestaurantConfigPanel_emailLab");
		container6.add(emailLab, new ColumnData(100));
		
		emailField = new TextField<String>();
		emailField.setEnabled(false);
		timeoutField.setId("RestaurantConfigPanel_emailField");
		container6.add(emailField,new ColumnData(150));
	}
	
	public void updateRestaurant() {
		commonConfigArea.outReserArea.setValue("demo-------");
		commonConfigArea.expDeliveryArea.setValue("demo======");
		commonConfigArea.nonValidDeliveryArea.setValue("test---");
		commonConfigArea.inRoomRequestTimeoutLcArea.setValue("---good");
		commonConfigArea.inRoomRequestTimeoutTempArea.setValue("ok");
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		CserviceControllerUtils.loadPropertys(propStore, prosComBox);
		updateRestaurant();
	}

	@Override
	public boolean onHide() {
		return true;
	}

}
